use std::cell::RefCell;

use crate::class_path::class_file_packet::ClassFilePacket;

pub struct CompositeClassFilePacket {
    pub class_file_packet_list: RefCell<Vec<Box<dyn ClassFilePacket>>>,
}

impl ClassFilePacket for CompositeClassFilePacket {
    fn read_class(&self, class_name: &String) -> Result<Vec<u8>, String> {
        for entry in self.class_file_packet_list.borrow_mut().iter() {
            let result = entry.read_class(class_name);
            if result.is_ok() {
                return result;
            }
        }
        return Err(String::from("Class Not Found"));
    }

    fn to_string(&self) -> String {
        let mut result = String::new();
        result.push_str("CompositeClassFilePacket[\n");
        for entry in self.class_file_packet_list.borrow_mut().iter() {
            result.push('\t');
            result.push_str(entry.to_string().as_str());
            result.push('\n');
        }
        result.push_str("]");
        return result;
    }
}